﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using DiscoveryLogic.Common.Utilities.Dispatcher;
using DiscoveryLogic.Workflows.Components.TextMining.Clustering;

namespace DiscoveryLogic.DocumentClustering
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args == null || args.Length <1 || args.Length>2)
            {
                WriteInfo("Correct format should be:");
                WriteError(string.Format("{0} {1} {2}",
                    "DocumentClustering.exe",
                    "doctermlist.settings",
                    "clustering.settings"));

                WriteInfo("or");
                WriteError(string.Format("{0} {1}",
                    "DocumentClustering.exe",
                    "clustering.settings"));

                WriteInfo(string.Format("Current argument count: {0}, first argument is: {1}",
                    args.Length,
                    args[0]));
            }
            else if(args.Length==2)
            {
                string docListSettingFilePath = args[0];
                XmlDocument xDoc1 = new XmlDocument();
                xDoc1.Load(docListSettingFilePath);
                XmlNode instructionNode1 = xDoc1.DocumentElement.ChildNodes[0];
                DocumentTermListInstruction instruction1 =
                    new DocumentTermListInstruction();
                instruction1 = (DocumentTermListInstruction)instruction1.Instantiate(instructionNode1);

                string clusteringSettingFilePath = args[1];
                XmlDocument xDoc2 = new XmlDocument();
                xDoc2.Load(clusteringSettingFilePath);
                XmlNode instructionNode2 = xDoc2.DocumentElement.ChildNodes[0];
                UnsupervisedClusteringInstruction instruction2 =
                    new UnsupervisedClusteringInstruction();
                instruction2 = (UnsupervisedClusteringInstruction)instruction2.Instantiate(instructionNode2);

                DocumentTermListWorker worker1 = new DocumentTermListWorker();
                worker1.SetInstruction(Guid.NewGuid(), instruction1, "Clustering", "Document term list");
                worker1.WorkerErredEvent +=
                    worker_WorkerErredEvent;
                worker1.WorkerStatusChangedEvent +=
                    worker_WorkerStatusChangedEvent;
                worker1.WorkerFinishedEvent += 
                    worker_WorkerFinishedEvent;
                worker1.DoWork();

                UnsupervisedClusteringWorker worker2 = new UnsupervisedClusteringWorker();
                worker2.SetInstruction(Guid.NewGuid(), instruction2,"Clustering","Document Clustering");
                worker2.WorkerErredEvent += worker_WorkerErredEvent;
                worker2.WorkerStatusChangedEvent += worker_WorkerStatusChangedEvent;
                worker2.WorkerFinishedEvent +=worker_WorkerFinishedEvent;
                worker2.DoWork();
            }
            else if(args.Length==1)
            {
                string clusteringSettingFilePath = args[0];
                XmlDocument xDoc2 = new XmlDocument();
                xDoc2.Load(clusteringSettingFilePath);
                XmlNode instructionNode2 = xDoc2.DocumentElement.ChildNodes[0];
                UnsupervisedClusteringInstruction instruction2 =
                    new UnsupervisedClusteringInstruction();
                instruction2 = (UnsupervisedClusteringInstruction)instruction2.Instantiate(instructionNode2);

                UnsupervisedClusteringWorker worker2 = new UnsupervisedClusteringWorker();
                worker2.SetInstruction(Guid.NewGuid(), instruction2, "Clustering", "Document Clustering");
                worker2.WorkerErredEvent += worker_WorkerErredEvent;
                worker2.WorkerStatusChangedEvent += worker_WorkerStatusChangedEvent;
                worker2.WorkerFinishedEvent += worker_WorkerFinishedEvent;
                worker2.DoWork();
            }

            Console.ReadLine();
        }

        static void worker_WorkerFinishedEvent(Guid ticketID, Type workerType, InstructionBase instructionBase, ResultBase resultBase)
        {
            WriteInfo(string.Format("{0} finished.",workerType.ToString()));
        }

        static void worker_WorkerStatusChangedEvent(Guid ticketID, Type workerType, InstructionBase instructionBase, 
            string workflowName, string componentName, string message, int percent)
        {
            WriteInfo(string.Format("{0}%\t{1}", percent, message));
        }

        static void worker_WorkerErredEvent(Guid ticketID, Type workerType, InstructionBase instructionBase, string error, bool isCritical)
        {
            WriteError(error);
        }

        #region display
        private static void WriteError(string error)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.BackgroundColor = ConsoleColor.Black;
            Console.WriteLine(error);
            Console.ForegroundColor = ConsoleColor.White;
            Console.BackgroundColor = ConsoleColor.Black;
        }

        private static void WriteInfo(string info)
        {
            Console.WriteLine(string.Format("{0}: {1}", DateTime.Now.ToShortTimeString(), info));
        }
        #endregion
    }
}
